package com.coamc.xlsunit;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RowOrmInputHolder extends RowInputHolder {

	static Log log = LogFactory.getLog(RowOrmInputHolder.class);

	public RowOrmInputHolder(XLSParser parser, VariableTable vars, String sheetName) {
		super(parser, vars, sheetName);

	}

	@Override
	protected void saveDb(Map<String, Object> record,List<String> pks, DBCallBack cb, XLSPoistion p) {
		// 开始存入数据库
		DBAccess access = parser.db;
		Map<String, Object> key = null;
		try {
			key = access.save(tableName, record,pks);
		} catch (RuntimeException ex) {
			throw new RuntimeException(" 保存数据" + this.tableName + " 出错，在" + p+" "+ex.getMessage(), ex);
		}

		if (cb.hasData()) {
			Map<String, Object> dbData = access.findById(tableName, key);
			for (DBCallBackItem col : cb.getVarRef()) {
				String varRef = col.getVarRef();
				String colName = col.getColName();
				if (!vars.contain(varRef)) {
					vars.add(varRef, dbData.get(colName));
					
				} else {
					// 已经赋值过了
					Object o = vars.find(varRef);
					// tmd 忘记业务场景了
					throw new RuntimeException("该变量已经赋值过了，不能再赋值，请用不同的变量名字");
					// ClassUtil.setValue(entity, write, o);
				}

			}
		}

	}

}